package com.jzcn.admin.controller.system;

import com.jzcn.admin.annotation.ServerLog;
import com.jzcn.admin.base.BaseController;
import com.jzcn.common.dto.system.resources.ResourcesDTO;
import com.jzcn.common.dto.system.resources.SearchResourcesDTO;
import com.jzcn.common.response.ResponseResult;
import com.jzcn.common.utils.JzPage;
import com.jzcn.service.SysResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(value = "Resources", tags = "菜单管理")
@RequestMapping("/api/resources")
public class ResourcesController extends BaseController {

    @Resource
    SysResourcesService sysResourcesService;

    @ApiOperation("菜单列表")
    @GetMapping("/page")
    @RequiresPermissions("system:resources:page")
    @ServerLog(value ="菜单列表", code= "system:resources:page")
    public ResponseResult<JzPage<ResourcesDTO>> list(SearchResourcesDTO searchResourcesDTO) {
        return ResponseResult.success(sysResourcesService.listPage(searchResourcesDTO));
    }


    @ApiOperation("菜单树形")
    @GetMapping("/tree")
    public ResponseResult<ResourcesDTO> tree() {
        return ResponseResult.success(sysResourcesService.treeData());
    }


    @ApiOperation("新增菜单")
    @PostMapping("/create")
    @RequiresPermissions("system:resources:create")
    @ServerLog(value ="新增菜单", code= "system:resources:create")
    public ResponseResult<Boolean> create(@RequestBody ResourcesDTO resourcesDTO) {
        return ResponseResult.success(sysResourcesService.create(resourcesDTO));
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:resources:delete")
    @ServerLog(value ="删除菜单", code= "system:resources:delete")
    public ResponseResult<Boolean> delete(@PathVariable("id") Integer id) {
        return ResponseResult.success(sysResourcesService.delete(id));
    }

    @ApiOperation("更新菜单")
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:resources:update")
    @ServerLog(value ="更新菜单", code= "system:resources:update")
    public ResponseResult<Boolean> update(@PathVariable("id") Integer id, @RequestBody ResourcesDTO resourcesDTO) {
        return ResponseResult.success(sysResourcesService.update(resourcesDTO, id));
    }

    @ApiOperation("启用/禁用")
    @PostMapping("/status")
    @RequiresPermissions("system:resources:status")
    public ResponseResult<Boolean> userStatus(@RequestParam("id") Integer id, @RequestParam Integer status){
        return ResponseResult.success(sysResourcesService.status(id,status));
    }


}
